package cn.jjxx.modules.sso.utils;

import java.util.HashMap;
import java.util.Map;

import org.framework.customutil.endecryption.aes.AESHelper;
import org.framework.customutil.endecryption.base64.BASE64Helper;
import org.framework.customutil.endecryption.des.DesHelper;
import org.framework.customutil.endecryption.rsa.RSAHelper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.modules.sso.entity.SSOLoginInfo;

/**
 * @Title: EnDecryptUtils.java .<br>
 * @Package cn.jjxx.modules.sso.utils .<br>
 * @Description: 加密解密参数工具类 .<br>
 * @author 郑成功 .<br>
 * @email a876459952@qq.com .<br>
 * @date 2020-3-11 上午9:00:20.<br>
 * @version V1.0.<br>
 */
public class EnDecryptUtils {

	/**
	 * @Description: 解密登录的参数信息 .<br>
	 * @param encryptParams map的加密字符串.<br>
	 * @param type 加密类型.<br>
	 * @param privateKey 私密，有就传，没有则不传.<br>
	 * @return SSOLoginInfo 返回登录参数信息.<br>   
	 * @author zcg .<br>
	 * @date 2020-3-11 上午11:01:51.<br>
	 */
	@SuppressWarnings("unchecked")
	public static SSOLoginInfo decryptParams(String encryptParams, EnDecryptType type, String privateKey){
		SSOLoginInfo info = new SSOLoginInfo();
		try {
			String result = null;
			if(type.equals(EnDecryptType.AES)){
				result = AESHelper.AESDncode(encryptParams, privateKey);
			}else if(type.equals(EnDecryptType.DES)){
				result = DesHelper.decrypt(encryptParams, privateKey);
			}else if(type.equals(EnDecryptType.BASE64)){
				result = BASE64Helper.decryptBASE64(encryptParams);
			}else if(type.equals(EnDecryptType.RSA)){
				result = RSAHelper.decrypt(encryptParams, RSAHelper.getPrivateKey(privateKey));		
			}
			info = getLoginInfo(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return info;
	}
	
	/**
	 * @Description: 将登录信息通过指定的方式加密成map字符串 .<br>
	 * @param loginInfo 登录信息.<br>
	 * @param type 加密类型.<br>
	 * @param publicKey 公钥，有就传，没有则不传.<br>
	 * @return 返回加密字符串.<br>   
	 * @author zcg .<br>
	 * @date 2020-3-11 上午11:03:19.<br>
	 */
	public static String encryptParams(SSOLoginInfo loginInfo, EnDecryptType type, String publicKey){
		String encryptParams = null;
		try {
			String params = loginInfoToMapStr(loginInfo);
			if(type.equals(EnDecryptType.AES)){
				encryptParams = AESHelper.AESEncode(encryptParams, publicKey);
			}else if(type.equals(EnDecryptType.DES)){
				encryptParams = DesHelper.encrypt(params, publicKey);
			}else if(type.equals(EnDecryptType.BASE64)){
				encryptParams = BASE64Helper.encryptBASE64(params);
			}else if(type.equals(EnDecryptType.RSA)){
				String centent = RSAHelper.encrypt(params, RSAHelper.getPublicKey(publicKey));
				encryptParams = centent;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encryptParams;
	}
	
	/**
	 * @Description: 将登录信息转换为map的字符串 .<br>
	 * @return map的字符串.<br>   
	 * @author zcg .<br>
	 * @date 2020-3-11 上午11:09:36.<br>
	 */
	private static String loginInfoToMapStr(SSOLoginInfo loginInfo){
		JSONObject json = new JSONObject();
		json.put("userId", loginInfo.getUserId());
		json.put("publicKey", loginInfo.getPublicKey());
		json.put("privateKey", loginInfo.getPrivateKey());
		json.put("redirectUrl", loginInfo.getRedirectUrl());
		return json.toJSONString();
	}
	
	/**
	 * @Description: 将map字符串转成单点登录实体数据 .<br>
	 * @param mapstr map字符串.<br>
	 * @return 登录实体信息.<br>   
	 * @author zcg .<br>
	 * @date 2020-3-11 上午10:45:24.<br>
	 */
	private static SSOLoginInfo getLoginInfo(String mapstr){
		SSOLoginInfo info = new SSOLoginInfo();
		HashMap<String,Object> map = JSON.parseObject(mapstr.toString(), HashMap.class);
		info.setUserId(ObjectUtils.isNullOrEmpty(map.get("userId"))?null:map.get("userId").toString());
		info.setPrivateKey(ObjectUtils.isNullOrEmpty(map.get("privateKey"))?null:map.get("privateKey").toString());
		info.setPublicKey(ObjectUtils.isNullOrEmpty(map.get("publicKey"))?null:map.get("publicKey").toString());
		info.setRedirectUrl(ObjectUtils.isNullOrEmpty(map.get("redirectUrl"))?null:map.get("redirectUrl").toString());
		return info;
	}
	
	
	/**
	 * @Title: EnDecryptUtils.java .<br>
	 * @Package cn.jjxx.modules.sso.utils .<br>
	 * @Description: 加解密类型 .<br>
	 * @author 郑成功 .<br>
	 * @email a876459952@qq.com .<br>
	 * @date 2020-3-11 上午9:05:43.<br>
	 * @version V1.0.<br>
	 */
	public static enum EnDecryptType{
		AES,DES,BASE64,RSA,SHA,MD5
	}
	
	/**
	 * @Description: 根据传入的字符串，获取枚举类型的加解密类型 .<br>
	 * @param secretType 加解密类型（AES/DES/BASE64/RSA/SHA/MD5）.<br>
	 * @return 返回加解密枚举类型.<br>   
	 * @author zcg .<br>
	 * @date 2020-3-12 下午2:47:56.<br>
	 */
	public static EnDecryptType getEnDecryptType(String secretType){
		EnDecryptType enDecryptType = null;
		switch (secretType) {
		case "AES":
			enDecryptType = EnDecryptType.AES;
			break;
		case "DES":
			enDecryptType = EnDecryptType.DES;
			break;
		case "BASE64":
			enDecryptType = EnDecryptType.BASE64;
			break;
		case "RSA":
			enDecryptType = EnDecryptType.RSA;
			break;
		case "SHA":
			enDecryptType = EnDecryptType.SHA;
			break;
		case "MD5":
			enDecryptType = EnDecryptType.MD5;
			break;
		}
		return enDecryptType;
	}
}
